<template>
  <div class="app-container">
    <div class="header flex">
      <FormSearch :formFields="searchList" @search="Search" @reset="Search"></FormSearch>
    </div>
    <div class="footer">
      <div class="flex justify-end mb-3">
        <a-button type="primary" @click="handleAdd">新增角色</a-button>
      </div>
      <div class="table-inner">
        <a-table :columns="columns" :data-source="state.data" rowKey="role_id" :pagination="state.pagination"
          @change="pageChange" :loading="state.loading" :scroll="{ x: 1200 }">
          <template #bodyCell="{ record, column }">
            <template v-if="column.dataIndex === 'actions'">
              <a-space size="small">
                <a-button type="link" :disabled="record.id === 1" @click="handleEdit(record)">编辑</a-button>
                <a-popconfirm title="你确定要删除吗?" ok-text="确认" cancel-text="取消" @confirm="confirmDel(record)">
                  <a-button type="link" :disabled="record.id === 1" danger>删除</a-button>
                </a-popconfirm>
              </a-space>
            </template>
          </template>
        </a-table>
      </div>
    </div>
    <!--  弹出框-->
    <CreateEdit ref="createRef" @model-success="refresh" />
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import CreateEdit from "./components/CreateEdit.vue";
import { list, del } from "@/api/admin/role";
import { message } from "ant-design-vue";

//弹出框的ref
const createRef = ref();
const columns = [
  {
    title: "id",
    dataIndex: "role_id",
    width: 100,
  },
  {
    title: "名称",
    dataIndex: "name",
    width: 200,
  },
  {
    title: "更新时间",
    dataIndex: "update_time",
    width: 200,
  },
  {
    title: "创建时间",
    dataIndex: "create_time",
    width: 200,
  },
  {
    title: "操作",
    dataIndex: "actions",
    fixed: "right",
    width: 100,
  },
];
//搜索框数据
const searchList = reactive([
  {
    type: "input",
    label: "名称",
    name: "name",
    value: "",
    placeholder: "请输入名称",
  },
]);

const state = reactive({
  data: [], //表格的数据
  loading: false,
  pagination: {
    total: 0,
    showSizeChanger: true, //是否页码可以改变
    current: 1, //当前xx页面
    pageSize: 10, //一页展示xx条数据
    showTotal: function (total) {
      return "总共" + total + "条数据";
    }, //总共xx条
    showQuickJumper: true,
  }, //分页数据
  searchParams: {
    page: 1,
    limit: 10,
  }, //搜索数据
});

//分页变动数据
const pageChange = (pagination) => {
  state.pagination.current = pagination.current;
  state.pagination.pageSize = pagination.pageSize;
  //搜索的参数
  state.searchParams.page = pagination.current;
  state.searchParams.limit = pagination.pageSize;
  //搜索
  getList();
};

//获取列表的数据
const getList = () => {
  state.loading = true;
  list({
    ...state.searchParams,
  }).then(function (res) {
    setTimeout(function () {
      state.loading = false;
    }, 500);
    if (res.code == 0) {
      state.loading = false;
      const data = res.data;
      state.data = data.data;
      state.pagination.total = data.total;
    }
  });
};
//添加
const handleAdd = function () {
  createRef.value.show();
};

//编辑
const handleEdit = function (record) {
  createRef.value.show(record);
};

//删除
const confirmDel = function (record) {
  del({ id: record.role_id }).then(function (res) {
    if (res.code == 0) {
      message.success(res.msg);
      //重新加载数据
      getList();
    }
  });
};
//搜索事件
const Search = (values) => {
  state.searchParams.page = 1;
  state.searchParams = {
    ...state.searchParams,
    ...values,
  };
  //搜索
  getList();
};

const refresh = () => {
  getList();
};
//获取列表
getList();
</script>

<style scoped lang="scss">
@import "./index";
</style>
